package cn.yunbamboos.mybatis.metadata;

import cn.yunbamboos.mybatis.annotation.TablePrimaryKey;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ResultFlag;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;

import java.lang.reflect.Field;
import java.util.Collections;

public class TablePrimaryKeyInfo extends BaseInfo {

    public TablePrimaryKeyInfo(Field field, TablePrimaryKey column) {
        super(field, column.value(), field.getName(), field.getType(), column.whereStrategy(), column.sort());
    }

    /**
     * 获取 ResultMapping
     *
     * @param configuration Configuration
     * @return ResultMapping
     */
    ResultMapping getResultMapping(final Configuration configuration) {
        ResultMapping.Builder builder = new ResultMapping.Builder(configuration, this.property, this.column, this.propertyType)
                .flags(Collections.singletonList(ResultFlag.ID));
        return builder.build();
    }

    ParameterMapping getParameterMapping(final Configuration configuration, final String resultMapId) {
        ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, this.property, this.propertyType);
        builder.resultMapId(resultMapId);
        builder.jdbcType(JdbcType.VARCHAR);
        return builder.build();
    }

}
